package main

func longestConsecutive(nums []int) int {
	m := make(map[int]struct{}, len(nums))
	for _, v := range nums {
		m[v] = struct{}{}
	}
	ret := 0
	m2 := make(map[int]int, len(nums)) // 从 v 开始连续数组的长度, 避免重复扫描
	for _, v := range nums {
		if _, ok := m[v-1]; ok {
			continue
		}
		v0 := v
		size := 1
		for {
			v++
			if s, ok := m2[v]; ok {
				size += s
				break
			}
			_, ok := m[v]
			if !ok {
				break
			}
			size++
		}
		m2[v0] = size
		if size > ret {
			ret = size
		}
	}
	return ret
}

func main() {
	println(longestConsecutive([]int{100, 4, 200, 1, 3, 2}))
}
